<template>
    <h2 style="text-align: center;">添加用药信息</h2>
    <table border="1" class="table table-bordered">
        <tbody>
            <tr>
                <th>药品名称</th>
                <th>
                    <input type="text" placeholder="请输入药品名称"  v-model="Info.Phice_Name"/>
                    <span style="color: red;">*</span>
                </th>
            </tr>

            <tr>
                <th>用药日期</th>
                <th>
                    <input type="date" v-model="Info.Phice_Time" />
                    <span style="color: red;">*</span>
                </th>
            </tr>

            <tr>
                <th>病人名称</th>
                <th>
                    <input type="text" placeholder="请输入病人名称" v-model="Info.Phice_UserName"/>
                    <span style="color: red;">*</span>
                </th>
            </tr>

            <tr>
                <th>用药状态</th>
                <th>
                   <input type="radio" :value="true" checked  v-model="Info.Phice_State"/>未开始
                   <input type="radio" :value="false" v-model="Info.Phice_State" />已完成
                </th>
            </tr>

            <tr>
                <th>配药医生</th>
                <th>
                    <input type="text" placeholder="请输入配药医生"v-model="Info.Phice_Doctor" />
                    <span style="color: red;">*</span>
                </th>
            </tr>

            <tr>
                <th>科室编号</th>
                <th>
                    <select v-model="Info.Dept_Id">
                        <option :value="-1">请选择</option>
                        <option v-for="item in InfoDept" :value="item.Dept_Id">{{ item.Dept_Name }}</option>
                    </select>
                </th>
            </tr>

            <tr>
                <th colspan="2" style="text-align: center;">
                    <input type="button" value="添加用药信息" class="btn btn-success" @click="AddPhice" style="margin-right: 10px;"/>
                    <a href="/ShowPhice">返回列表</a>
                </th>
            </tr>
        </tbody>
    </table>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import { useRouter } from 'vue-router';
    
    const router=useRouter();
    //添加响应式数据
    const Info=ref({
        Phice_Name: "",
        Phice_Time: "",
        Phice_UserName: "",
        Phice_State: true,
        Phice_Doctor: "",
        Dept_Id: -1
    })
    //科室响应式数据
    const InfoDept=ref([{
        Dept_Id: 0,
        Dept_Name: "",
    }])
    onMounted(()=>{
        GetDept();//获取科室信息
    })
    //获取科室信息
    const GetDept=()=>{
        axios.get("https://localhost:44339/api/Phice/GetDept")
        .then(res=>{
            InfoDept.value=res.data
        })
    }

    //添加用药信息
    const AddPhice=()=>{
        //非空验证
        if(Info.value.Phice_Name=="")
        {
            alert('药品名称不能为空!');
            return;
        }

        if(Info.value.Phice_Time=="")
        {
            alert('用药日期不能为空!');
            return;
        }

        if(Info.value.Phice_UserName=="")
        {
            alert('病人名称不能为空!');
            return;
        }

        if(Info.value.Phice_Doctor=="")
        {
            alert('配药医生不能为空!');
            return;
        }
        axios.post("https://localhost:44339/api/Phice/AddPhice",Info.value)
        .then(res=>{
            if(res.data>0)
            {
                alert('用药信息添加成功');
                router.push("/ShowPhice");
            }
            else
            {
                alert('用药信息添加失败');
            }
        })
    }
</script>